/**
 * EditorRecordManager.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.servlets.actions;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import extjsdyntran.servlets.Constants;
import extjsdyntran.translation.ITranslationService;


/**
 * @description Main class for Translation Editor Grid to support record deletion/creation/update
 * 			    Used only from EditorActions
 */
final class EditorRecordManager {

	final static Logger logger = LoggerFactory.getLogger(EditorRecordManager.class);
	private static Gson gs= new Gson();
	
	private static String getError(Exception e){
		try{
			EditorResponse er = new EditorResponse();
			er.setSuccess(false);
			er.setMsg(e.getMessage());
			return gs.toJson(er);
			}catch(Exception e1){
				logger.error("Error converting to JSON...",e1);
				return Constants.json_false;
			}
	}
	
	
	private static EditorResponse getResponse(boolean status, String msg){
		EditorResponse er = new EditorResponse();
		er.setSuccess(status);
		er.setMsg(msg);
		return er;
		
	}
	
	//creates Ext json records for loading in grid
	public static String getTranslationRecords(ITranslationService service , String language) {
		try {

			Properties lang = service.getTranslations(language);
			EditorResponse er = getResponse(false,Constants.err1);		
			if (service == null){
				return gs.toJson(er);
			}
			er.reset();
			er.setSuccess(true);
			er.setTranslation(lang);
			er.setLanguage(language);
			return gs.toJson(er);

		} catch (Exception e) {
			logger.error("Error converting to JSON...",e);
			return getError(e);
		}
	}



	//for each received record delete through TranslationFactory
	//TranslationFactory will search for appropriate service supporting requested language
	//and then it will invoke delete on that service
	//TODO store errors in array an set message with errors to send to the editor
	public static String deleteRecords(ITranslationService service , String language, String values) {
		try {

			EditorResponse er = getResponse(false,Constants.err1);	 
			
			JsonElement je = new JsonParser().parse(values);
			if (je.isJsonPrimitive()){				
				er.reset();
				boolean result =service.deleteTranslation(language, je.getAsString());
				er.setSuccess(result);
				if(!result) er.setMsg(Constants.err14);
				return gs.toJson(er);  
			}
			
			
			EditorRecord []data = null;
			try{
				data = gs.fromJson(values, EditorRecord[].class);
			}catch(Exception e){
					String[] keys = gs.fromJson(values, String[].class);
					for(String key : keys){
						service.deleteTranslation(language,  key);
					}				 
				er.reset();
				er.setSuccess(true);
				return gs.toJson(er);
			}

			int j = 0;
			while (j<data.length){	
				service.deleteTranslation(language,  data[j].getKey());
				j++;
			};			
			er.reset();
			er.setSuccess(true);
			return gs.toJson(er);

		} catch (Exception e) {
			logger.error("Error converting to JSON...",e);
			return getError(e);
		}		
		
	}

	public static String updateRecords(ITranslationService service , String language, String values) {
		return createRecords(service,language,values);			
	}
	
	
	
	public static String createRecords(ITranslationService service , String language, String values) {
		try {

			EditorResponse er = getResponse(false,Constants.err1);	 	
			if (service == null)
				return gs.toJson(er);
			
			 er.reset();
			 er.setSuccess(true);
			 Collection<EditorRecord> data = null;
			 Type mapType =   new TypeToken<Collection<EditorRecord>>() {}.getType();
			 try{
				 data = gs.fromJson(values, mapType);
				 er.getData().addAll(data);
			 }catch(Exception e){
				EditorRecord rc = gs.fromJson(values, EditorRecord.class);
				er.addRecord(rc);
			 }
				
			 Iterator<Object> it = er.getData().iterator();
			 while(it.hasNext()){
				 EditorRecord rc = (EditorRecord) it.next();
				 service.saveTranslation(language, rc.getKey(), rc.getValue());
			 }
			 er.setSuccess(true);
			 return gs.toJson(er);

		} catch (Exception e) {
			logger.error("Error converting to JSON...",e);
			return getError(e);
		}		
			
	}	
	
}
